<template>
  <div>
    <!-- 学科列表表头盒子 -->
    <el-card>
      <el-form :model="form" inline ref="form">
        <el-form-item label="学科编号" prop="rid">
          <el-input v-model="form.rid"></el-input>
        </el-form-item>
        <el-form-item label="学科名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="创建者" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select placeholder="请选择状态" v-model="form.status">
            <el-option value="0" label="禁用"></el-option>
            <el-option value="1" label="启用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="reset">清除</el-button>
          <el-button type="primary" @click="add">+新增学科</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 学科列表table区域 -->
    <el-card class="subjectList">
      <el-table :data="subjectList">
        <el-table-column label="序号" width="80px">
          <template slot-scope="scope">{{pagination.pageSize*(pagination.currentPage-1)+scope.$index+1}}</template>
        </el-table-column>
        <el-table-column prop="rid" label="学科编号" width="150px"></el-table-column>
        <el-table-column prop="name" label="学科名称" width="300px"></el-table-column>
        <el-table-column prop="short_name" label="简称" width="150px"></el-table-column>
        <el-table-column prop="username" label="创建者" width="200px"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" width="300px"></el-table-column>
        <el-table-column prop="status" label="状态" width="100px">
          <template slot-scope="scope">
            <span :class="{red:scope.row.status==0}">{{scope.row.status==0 ? '禁用':'启用'}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" @click="resetStatus(scope.row.id)">{{scope.row.status == 1? '禁用':'启用'}}</el-button>
            <el-button type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <!-- 分页区域 -->
        <el-pagination background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pagination.currentPage"
          :page-sizes="pagination.pageSizes"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
        ></el-pagination>
      </div>
    </el-card>
    <subjectAdd ref="subjectAdd"></subjectAdd>
  </div>
</template>

<script>
import { getSubjectList,resetStatus,delSubject } from "@/api/subject.js";
import subjectAdd from './subjectAdd.vue';
export default {
  components: {
    subjectAdd,
  },
  data() {
    return {
      form: {
        rid: "", //否	string	学科编号
        name: "", //否	string	学科名称
        username: "", //否	string	创建者用户名
        status: "", //否	string	状态 0(禁用) 1(启用)
      },
      // 获取的学科列表数据
      subjectList: [],
      pagination: {
        total: 0,
        currentPage: 1,
        pageSizes: [1,10, 20, 30],
        pageSize: 10,
      },
    };
  },
  created() {
    this.getData()
  },
  methods: {
    //切换页容量
    handleSizeChange(size) {
      this.pagination.pageSize = size;
      this.pagination.currentPage = 1;
      this.getData();
    },
    //切换页码
    handleCurrentChange(page) {
      this.pagination.currentPage = page;
      this.getData();
    },
    // 请求学科列表
    getData() {
      let query = {
        ...this.form,
        page: this.pagination.currentPage,
        limit: this.pagination.pageSize,
      };
      // 页面加载，即发送获取学科列表的请求
      getSubjectList(query).then((res) => {
        // console.log(res);
        this.subjectList = res.data.data.items;
        this.pagination.total = res.data.data.pagination.total;
      });
    },
    // 搜索按钮
    search(){
      //切换当前页为第一页，并发送请求
      this.pagination.currentPage = 1;
      this.getData();
    },
    // 清除按钮：清除筛选
    reset(){
      // 重置form表单
      this.$refs.form.resetFields();
      this.search();
    },
    // 修改状态
    resetStatus(id){
      resetStatus({id}).then(()=>{
        this.$message.success('修改成功');
        this.getData();
      })
    },
    // 新增学科
    add(){
      this.$refs.subjectAdd.isShow = true;
      this.$refs.subjectAdd.mode = 'add';
    },
    //删除学科
    del(id){
      this.$confirm('确定删除吗','提示',{
        confirmButtonText:'确定删除',
        cancelButtonText:'取消删除',
        type:'warning'
      }).then(()=>{
        delSubject({id}).then(()=>{
          this.$message.success('删除成功');
          this.search();
        })
      }).catch(()=>{})
    },
    //编辑学科
    edit(row){
      this.$refs.subjectAdd.form = JSON.parse(JSON.stringify(row));
      this.$refs.subjectAdd.mode = 'edit';
      this.$refs.subjectAdd.isShow = true;
    }
  }
};
</script>

<style lang="less">
.subjectList {
  margin-top: 20px;
  .red {
    color: red;
  }
  .page {
    text-align: center;
    margin-top: 30px;
  }
  .el-table th,
  .el-table__body td{
    text-align: center;
  }
}
.el-dialog__header{
        padding: 0;
    }
</style>